﻿import mx.utils.Delegate;
import arkadium.EventManager;
import arkadium.SoundManager;
import game.Main;
import game.Board;
import game.DataManager

class game.GameView extends arkadium.GUIPanel{
	private var _smcBoard:Board;
	private var _stxScore:TextField;
	private var _sbtEndGame:Button;
	private var _fnDelegateFunction:Function;
	
	
	private function GameView(){
		super.registerPanel( this, "GAME_VIEW" );
		
		_sbtEndGame.onRelease =  Delegate.create(this, endGame); // Damian
		_sbtEndGame.onRollOver = Delegate.create( Main.getInstance(),  Main.getInstance()._cSoundManager.rolloverSoundStart ); // Damian
		
	}
	
	public function onInitialize(){
		//super.showMe( true );
		
		Main.getInstance()._cEventManager.addEventListener( "START_GAME", Delegate.create( this, startGame ) );
		Main.getInstance()._cEventManager.addEventListener( "RESTART_GAME", Delegate.create( this, startGame) );
		//EventManager.getInstance().addEventListener( "END_LOADED", _fnDelegateFunction =Delegate.create( this, onEndLoaded ) ); 
		_smcBoard.onInitialize();
	}
	
	private function startGame(){
		_fnDelegateFunction = Delegate.create( this, onEndLoaded );
		Main.getInstance()._cEventManager.addEventListener( "END_LOADED", _fnDelegateFunction ); 
		super.showMe( true );
		_smcBoard.onReset();
	}
	
	public function setScore( nScore:Number ){
		_stxScore.text = nScore.toString();
		DataManager.getInstance().setScore( nScore );
	}
	
	private function endGame(){
		//EventManager.getInstance().broadcastMessage( "END_LOADED" );
		Main.getInstance()._cEventManager.broadcastMessage( "END_GAME" );
		trace("endGame");
		//super.showMe( false );
	}
	
	private function onEndLoaded(){
		super.showMe( false );
		Main.getInstance()._cEventManager.removeEventListener( "END_LOADED", _fnDelegateFunction ); 
	}
	
}